//
//  WorkflowProvider.m
//  TarPhone
//
//  Created by Christopher Anderson on 16/03/2009.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//

#import "WorkflowProvider.h"


@implementation WorkflowProvider

@synthesize currentWorkFlow;
@synthesize workflows;

- (id) init {
	
	self = [super init];
	
	if ( self ) {
			self.workflows = [[NSMutableArray alloc] initWithCapacity:10];
	}
	
	return self;
}


- (NSArray*) getWorkFlows {
	[self makeRequest:@"workflow/shortinfo/all" parameters:nil];
	return workflows;
}

- (void)parserDidStartDocument:(NSXMLParser *)parser {
    [self.workflows release];
	self.workflows = [[NSMutableArray alloc] initWithCapacity:10];
}


- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {
    if (qName) {
        elementName = qName;
    }
	
	if ([elementName isEqualTo:@"item"]) {
		self.currentWorkFlow = [[Workflow alloc] init];
		[self.workflows addObject:self.currentWorkFlow];
		return;
	}
	
	if ([elementName isEqualToString:@"var"]) {
		NSString *name = [attributeDict valueForKey:@"name"];
		NSString *value = [attributeDict valueForKey:@"value"];
		if ([name isEqualToString:@"_id"]) {
			self.currentWorkFlow._id = value;
		} else if ([name isEqualToString:@"title"]) {
			self.currentWorkFlow.title = value;
		} else if ([name isEqualToString:@"description"]) {
			self.currentWorkFlow.description = value;
		} else if ([name isEqualToString:@"receptor"]) {
			self.currentWorkFlow.receptor = value;
		} else if ([name isEqualToString:@"token"]) {
			self.currentWorkFlow.token = value;
		}
	}	
}

@end
